/**
 *
 * 事务操作 demo 项目中没用到该文件
 *
 */
var db = require('./mysql.js');
var async = require('async');

db.getConnection(function (connection) {
    connection.beginTransaction(function (err) {
        if (err) {
            console.log(err);
            return;
        }

        var task1 = function (callback) {
            connection.query(`insert into user (name) values('a')`, function (err, result) {
                if (err) {
                    console.log(err);
                    callback(err, null);
                    return;
                }
                console.log('第一次插入成功!');
                callback(null, result);
            })
        }
        var task2 = function (callback) {
            connection.query(`insert into user (name) values('b')`, function (err, result) {
                if (err) {
                    console.log(err);
                    callback(err, null);
                    return;
                }
                console.log('第二次插入成功!');
                callback(null, result);
            })
        }
        var task3 = function (callback) {
            connection.query(`insert into user (name) values('c')`, function (err, result) {
                if (err) {
                    console.log(err);
                    callback(err, null);
                    return;
                }
                console.log('第三次插入成功!');
                callback(null, result);
            })
        }

        async.series([task1, task2, task3], function (err, result) {
            if (err) {
                console.log(err);
                //回滚
                connection.rollback(function () {
                    console.log('出现错误,回滚!');
                    //释放资源
                    connection.release();
                });
                return;
            }
            //提交
            connection.commit(function (err) {
                if (err) {
                    console.log(err);
                    return;
                }

                console.log('成功,提交!');
                //释放资源
                connection.release();
            });
        })
    });
})

/*
const connectHandler = require('./config') //引入上面所讲的数据库基础配置

const insertHandler = async (vals) => {
    const connection = await connectHandler() // 得到链接
    const tablename = 'xb_user_login'

    //开启事务
    return new Promise((resolve, reject) => {
        connection.beginTransaction(err => {
            if (err) {
                reject('开启事务失败'+err)
            } else {
                //执行INSERT插入操作
                connection.query(`select id from ${tablename}`, vals, (e, rows, fields) => {
                    if (e) {
                        return connection.rollback(() => {
                            console.log('插入失败数据回滚');
                        });
                    } else {
                        // 提交事务
                        connection.commit((error) => {
                            if (error) {
                                reject('事务提交失败'+error)
                            }
                        })
                        console.log(rows);
                        resolve(rows);
                        connection.release()  // 释放链接
                    }
                })
            }
        })
    })
}
module.exports = insertHandler;*/
